# -*- coding:utf-8 -*-

"""
DESC:
auth:lt
date:2023-04-21
"""
import json
import time

import pymysql
from datetime import datetime
from collections import defaultdict

# 分页设置，一页处理多少条记录
page_size = 100
# 最后一次id是多少，从哪一个自增id开始
last_id = 0
import requests


def get_crowd_ids(data, type):
    url = "http://api-hoder.skysrt.com/v2/search/isDevicePolicy"
    # url = "http://129.204.166.201/search/isDevicePolicy"
    payload = f'{type}={data}&uniqueFlag=true&policyIds=3456,3008&reqSource=21&devInfo=%7B%7D'
    print(payload)
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    response = requests.post(url, headers=headers, data=payload)

    # Check status code
    if response.status_code != 200:
        print(f"Request to {url} failed with status code {response.status_code}")
        return None

    # Try to parse JSON
    try:
        response_data = response.json()
        print(response_data)
        if isinstance(response_data, dict):
            # Check 'code' field
            if response_data.get('code') != '1000':
                print(f"Request to {url} with {type} = {data} failed with code {response_data.get('code')}")
                return None
            datares = response_data.get("data", {})
            result_data = datares.get("result", [])
            if len(result_data) > 0:
                return [str(item.get("crowdId", "")) for item in result_data]
            else:
                print(f"No result data from {url} for {type} = {data}")
                return None
        else:
            print(f"Unexpected response type from {url}: {type(response_data)}")
            return None
    except json.JSONDecodeError:
        print(f"Failed to parse JSON response from {url}")
        return None


def calculate_similarity(list1, list2):
    set1 = set(list1)
    set2 = set(list2)
    return len(set1 & set2) / len(set1 | set2)


def main():
    # Database connection setup
    connection = pymysql.connect(
        host="172.20.150.109",
        port=3307,
        user="test_dmp",
        passwd="DghHC3lFM1KzT3ZJ",
        db="proactive_service_conf",
        charset="utf8"
    )

    last_id = 6715101  # Initialize to infinity
    rowsNumber = 100000
    min_id = last_id - rowsNumber
    page_size = 100

    while True:
        with connection.cursor() as cursor:
            sql = f"SELECT id, mac, va_id FROM life_assistant_devices WHERE id<{last_id} ORDER BY id DESC LIMIT {page_size}"
            cursor.execute(sql)
            results = cursor.fetchall()

            if not results:
                break  # Exit the loop if no more results

            last_id = results[-1][0]  # Update last_id to the last record's id in this batch

            if last_id <= min_id:
                break

            for result in results:
                id, mac, va_id = result

                if va_id is None and mac is None:
                    continue

                if va_id is None:
                    continue


                time.sleep(0.04)  # Wait for 40ms
                mac_result = get_crowd_ids(mac, "mac")
                time.sleep(0.04)  # Wait for 40ms
                va_id_result = get_crowd_ids(va_id, "vaid")

                # If either request failed, skip this iteration
                if mac_result is None:
                    mac_result = ["0"]
                if va_id_result is None:
                    va_id_result = ["0"]

                similarity = calculate_similarity(mac_result, va_id_result)
                insert_sql = "INSERT INTO device_similarity (mac, vaid, mac_crowd_ids, vaid_crowd_ids, similarity) VALUES (%s, %s, %s, %s, %s)"
                cursor.execute(insert_sql, (mac, va_id, ','.join(mac_result), ','.join(va_id_result), similarity))

            connection.commit()
    connection.close()


if __name__ == "__main__":
    main()
